package client.socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class SocketClient {

	private static final String CLOSE = "close";// 断开标志

	private Socket socket;// 客户socket
	private BufferedReader reader;
	private BufferedWriter writer;

	private ReaderThread readerThread;// 获取服务器发送过来的消息

	private SimpleStringProperty receMsg;// 收到的消息
	private SimpleIntegerProperty isConnected;// 是否成功连接到服务器

	private SimpleDateFormat time = new SimpleDateFormat("yy-MM-dd HH:mm:ss");

	public SocketClient(SimpleStringProperty receMsg, SimpleIntegerProperty isConnected) {
		this.receMsg = receMsg;
		this.isConnected = isConnected;
	}

	/**
	 * 连接到服务器
	 */
	public void connect() {
		Thread t = new Thread() {
			public void run() {
				while (true) {
					try {
						isConnected.set(2);
						socket = new Socket("127.0.0.1", 888);
						isConnected.set(1);

						reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
						writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

						readerThread = new ReaderThread();
						readerThread.start();
						break;
					} catch (IOException e) {
						// e.printStackTrace();
						//System.out.println("连接中。。。");
					}
				}
			}
		};
		t.start();

	}

	/**
	 * 断开连接
	 */
	public void close() {
		try {
			writer.write(CLOSE);
			writer.newLine();
			writer.flush();
			writer.close();
			reader.close();
			socket.close();
			isConnected.set(0);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 发送消息
	 * 
	 * @param msg
	 */
	public void sendMesssage(String msg) {
		try {
			writer.write(msg, 0, msg.length());
			writer.newLine();
			writer.flush();
			receMsg.set(time.format(new Date()) + " 客户端\n" + msg.trim() + "\n");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取消息
	 * 
	 * @author huangyk
	 *
	 */
	class ReaderThread extends Thread {
		@Override
		public void run() {
			try {
				if (reader != null) {
					while (true) {
						String line = reader.readLine();
						if (line != null) {
							if (line.trim().equals(CLOSE)) {
								close();
								receMsg.set("server close " + time.format(new Date()) + "\n");
								break;
							}
							receMsg.set(time.format(new Date()) + " 服务器\n" + line + "\r\n");
						}
					}

				}
			} catch (Exception e) {
				// System.out.println("服务器断开连接");
				// e.printStackTrace();
				close();

			}
		}

		/**
		 * 关闭断开连接
		 */
		public void close() {
			try {
				writer.close();
				reader.close();
				socket.close();
				isConnected.set(0);
				
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
}
